package service;

import dao.UserDao;
import model.User;

import java.sql.SQLException;

public class UserService {
    private UserDao uDao = new UserDao();

    //注册功能
    public boolean register(User user) {
        try {
            //用户是否注册
            if (uDao.isUsernameExist(user.getUsername())) {
                return false;
            }
            if (uDao.isEmailExist(user.getEmail())) {

            }
            //邮箱是否注册
            uDao.addUser(user);
            return true;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return false;
    }

    //    登录业务
    public User login(String ue, String password) {
        User user = null;
//        1.username password 登录方式
        try {
            user = uDao.selectUsernamePassword(ue, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (user != null) {

            return user;
        }
//        2.email password 登录方式
        try {
            user = uDao.selectEmailPassword(ue, password);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        if (user != null) {
            return user;
        }
        return null;
    }

    public void updateUserAddress(User loginUser) {
    }

    public void updatePwd(User u) {

    }
}
